package com.genius.adminmanager.other.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.role.entity.RoleJson;
import com.genius.adminmanager.role.entity.RoleUrlJson;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools2;

@Controller
public class MainController {

	/**
	 * 跳转到主页面
	 */
	@GetMapping("/main")
	public String toMain(){
		return "/views/main";
	}
	@GetMapping("/adminmanager/main")
	public String toAdminmanagerMain(){
		return "/views/main";
	}
	/**
	 * 前台首页
	 * @throws Exception
	 */
	@GetMapping("/homepage")
	public String homepage(HttpSession session)throws Exception{
		String defaultUrl = "/views/unite_platform/homepage";
		String roleId = (String)session.getAttribute(RoleJson.roleId);
		try {
			if(roleId != null){
				String selectField = RoleUrlJson.url;
				String table = "t_role_url";
				String whereField = RoleUrlJson.roleId+","+RoleUrlJson.status;
				JSONObject json = new JSONObject();
				json.put(RoleUrlJson.roleId, roleId);
				json.put(RoleUrlJson.status, 1);
				JSONObject url = DBTools2.select(selectField, table, whereField, json);
				if(url != null){
					defaultUrl = url.getString(RoleUrlJson.url);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defaultUrl;
	}
	@ResponseBody
	@GetMapping("/main/loadOnceMenuInfo")
	public JSONObject loadOnceMenuInfo(HttpSession session){
		
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		String roleId = (String)session.getAttribute(SessionJson.roleId);
		if(roleId == null){
    		response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_Session_Failure);
    		return response;
    	}
		//先根据角色找到权限菜单id
		response = MenuUtils.getMenuOnceListByRoleId(roleId,response);
		return response;
	}
	@ResponseBody
	@PostMapping("/main/loadOnceMenuInfoById")
	public JSONObject loadOnceMenuInfoById(@RequestBody JSONObject json,HttpSession session){
		String menuId = json.getString(MenuJson.menuId);
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		String roleId = (String)session.getAttribute(SessionJson.roleId);
		System.out.println("=======================================");
		System.out.println(roleId);
		if(roleId == null){
    		response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_Session_Failure);
    		return response;
    	}

		try {
				JSONArray leftMenu = MenuUtils.getOnceMenuById(roleId,menuId);
				response.put(ResponseJson.status, ResponseJson.status_Success);
				response.put(ResponseJson.data, leftMenu);
		} catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, "查询菜单出错");
		}
		return response;
	}
	@GetMapping("/main/show")
	public String toEdit(){
		return "/views/mode";
	}
}

